library(ggplot2)

#Generates ggplot2 default colors
gg_color_hue <- function(n) {
  hues = seq(15, 375, length = n + 1)
  hcl(h = hues, l = 65, c = 100)[1:n]
}

paneld <- read.csv("age.csv",header=T, stringsAsFactors=FALSE)
paneld$pe <- paneld$c1
paneld$se <- paneld$c2
paneld$lb <- paneld$pe- 1.96*paneld$se
paneld$ub <- paneld$pe+ 1.96*paneld$se

#Subgroup Factors
paneld$Age <- factor (paneld$c3, levels=seq(1,2), 
                         labels= c("Youth","Non-Youth"))

paneld$group <- factor(paneld$c4,levels=seq(10,1),
                       labels=c("System Support",
                                "Class-Based Injustice Index",
                                "Contributor to Education Inequality:
                                \n Poor Families Do Not Value Education
                                \n as Much as Richer Families",
                                "Contributor to Education Inequality:
                                \n Systemic Injustice Perpetuates
                                \n Inequity Throughout Society",
                                "Agreement That Low Income Students
                                \n Have Same Opportunities as
                                \n High Income Students",
                                "Racial Resentment Index",
                                "Satisfaction with
                                \n Treatment of Minorities Index",
                                "Skin Color
                                \n Implicit Association Test",
                                "Feel Close to Blacks",
                                "Feel Close to Hispanics")[10:1])

f1 = ggplot(data=paneld, mapping=aes(x=group, y=pe, ymin=lb, ymax=ub, width= 0.5, shape=Age, color= Age)) + geom_pointrange(size= 0.25, position = position_dodge(width=.5))
f1 = f1 + scale_shape_manual(name="Age Group",values=c(5,6), labels=paneld$Age)  
f1= f1 + scale_color_manual(name="Age Group",values=gg_color_hue(2), labels=paneld$Age)
f1 = f1 + scale_fill_manual(values=c("black","black"))

#Error Bars
f1 = f1+ geom_errorbar(aes(ymin= paneld$pe - paneld$se, ymax= paneld$pe + paneld$se), width=.1, position=position_dodge(.5))

f1 = f1 + coord_flip()
f1 = f1 + geom_hline(yintercept=0, linetype="longdash", size=0.5)
f1 = f1 + labs( y= "Treatment Effect", x = "Dependent Variable", color = "Category")

#Reverse Legend
f1= f1 + guides(colour = guide_legend(reverse=TRUE),shape = guide_legend(reverse=TRUE))

#Limits - Set this to whatever you'd like limits to be.
#f1 = f1 + scale_y_continuous(limits = c(-0.2,0.2))

#Theme
f1 = f1+ theme(plot.background= element_rect(fill="white"),
               panel.background= element_rect(fill="white", color= "grey75"),
               panel.grid.major = element_line(color= "grey75"),
               panel.grid.minor = element_line(color= "grey80"),
               panel.grid.major.y = element_blank(),
               panel.grid.minor.y = element_blank(),
               panel.spacing.x = unit(12,"point"),
               axis.text = element_text(lineheight=0.5),
               legend.background = element_rect(fill="white")
)

print(f1)